function piZhun(ev, ok) {
        ev.stopPropagation();
        ok = (typeof ok != 'undefined') ? ok : false;
        if (ok) return true;
        $.blockUI({ message: $('#question_pizhun'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
        return false;
}

function confirmDelivery(ev, ok) {
        ev.stopPropagation();
        ok = (typeof ok != 'undefined') ? ok : false;
        if (ok) return true;
        $.blockUI({ message: $('#question_confirm'), css: { width: '400px',height:'100px',border:'10px solid #999999'} });
        return false;
}

function juJue(ev, ok) {
        ev.stopPropagation();
        ok = (typeof ok != 'undefined') ? ok : false;
        if (ok) return true;
        $.blockUI({ message: $('#question_jujue'), css: { width: '400px',height:'200px',border:'10px solid #999999'} });
        return false;
}

function notifyDelivery(ev, ok) {
        ev.stopPropagation();
        ok = (typeof ok != 'undefined') ? ok : false;
        if (ok) return true;
        $.blockUI({ message: $('#notify_delivery_question'), css: { width: '400px',height:'270px',border:'10px solid #999999'} });
        return false;
}

$(document).ready(function() {

	$('#pizhun').bind('submit', piZhun);
	$('#jujue').bind('submit', juJue);
	$('#notify_deliver_form').bind('submit', notifyDelivery);
	$('#delivery_confirm').bind('submit', confirmDelivery);

        $('#yes_jujue').click(function() {
		document.getElementById("reject_comment_hidden").value = document.getElementById("reject_comment").value;
                $('#jujue').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;float:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });
        $('#yes_pizhun').click(function() {
                $('#pizhun').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });
        $('#yes_confirm').click(function() {
                $('#delivery_confirm').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });
        $('#yes_notify').click(function() {
		document.getElementById("fahuo_comment_hidden").value = document.getElementById("fa_comment").value;
		var datetime = $('#fa_time').datetimebox('getValue');
		document.getElementById("fahuo_address_hidden").value = document.getElementById("fa_address").value;
		document.getElementById("fahuo_time_hidden").value = datetime;
		
		var selectedValues="";
		$checkedCheckboxes = $("input:checkbox[name=fahuo_university[]]:checked");
		$checkedCheckboxes.each(function () {
			selectedValues +=  $(this).val() +",";
		});
		document.getElementById("fahuo_university_hidden").value = selectedValues;
                $('#notify_deliver_form').trigger('submit', [true]);
                $.blockUI({ message: "<div style=\"margin-left:50px;flaot:left;margin-top:15px;font-size:14px;color:#999999;\">处理中...请不要关闭该页面！</div>" , css:{height:'50px',width:'400px',border:'10px solid #999999'}});
        });

        $('#no_pizhun').click(function() {
                $.unblockUI();
                return false;
                });
        $('#no_jujue').click(function() {
                $.unblockUI();
                return false;
                });
        $('#no_notify').click(function() {
                $.unblockUI();
                return false;
                });
        $('#no_confirm').click(function() {
                $.unblockUI();
                return false;
                });
	$.ajax({
                type: "POST",
                url: "/stores/time_out",
                data: {
                tuan_id: $("#tuanid").val(),
                tuanstatus: $("#tuanstatus").val(),
                type: "get"},
                success: function(time_out)
                {
                        if (time_out != '0') {
                                var date = new Date();
                                var parts = String(time_out).split(/[- : T]/);

				var month = parseInt(parts[1]) - 1;
				var day = parseInt(parts[2]);
                                date.setFullYear(parts[0], month, day);
                                date.setHours(parseInt(parts[3]));
                                date.setMinutes(parseInt(parts[4]));
                                date.setSeconds(parseInt(parts[5]));
                                date.setMilliseconds(0);
                                $('#compact').countdown({until:date, compact:true,description:''});
                        }
                }
        });

	var items_on_page = 10;
        var pages_step = 5;

        get_data_comments(1, 10);
        $.ajax({
                type: "POST",
                url: "/tuans/comments_total",
                data: {
                tuan_id: $("#tuanid").val(),
                type: "get"},
                success: function(total)
                {
                        $("#paging_comments").jpaging({
                                all_items_num: total,
                                callback: get_data_comments,
                                items_on_page: items_on_page,
                                pages_step: pages_step
                        });
                }
        });

        function get_data_comments(start_index, end_index){
                start_index = start_index - 1;
                end_index = end_index - 1;
                $.ajax({
                        type: "POST",
                        url: "/tuans/comments_paging",
                        data: {start_index: start_index,
                        end_index: end_index,
                        tuan_id: $("#tuanid").val(),
                        type: "get"},
                        success: function(html)
                        {
                                $("#demo_tbl_comments").html(html);
				$.prettyDate.now = function() {
                                        return new Date();
                                }
                                $("a").prettyDate();

                        }
                });
        }

	$("#tuan_add_list_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/tuans/add_shopping_list",
			data: {tuanid: $("#tuanid").val(),
			category: $("#tuancategory").val(),
			type: "get"},
			success: function(html)
			{
				$("#tuan_shopping_list").html(html);
			}
		});
	});

	$("#post_comment_button").click(function(){
		var comment = $("#tuan_comment_text").val();
		if(comment.length > 0) {
			$("#tuan_comment_text").attr("value", '');
			comment = comment.replace('\n', '<br />');
			$.ajax({
				type: "POST",
				url: "/tuans/comment",
				data: {
				comm_id: $("#comm_id").val(),
				tuan_id: $("#tuanid").val(),
				comment: comment,
				type: "get"},
				success: function(html)
				{
					get_data_comments(1, 10);
					$.ajax({
						type: "POST",
						url: "/tuans/comments_total",
						data: {
						tuan_id: $("#tuanid").val(),
						type: "get"},
						success: function(total)
						{
							$("#paging_comments").jpaging({
								all_items_num: total,
								callback: get_data_comments,
								items_on_page: items_on_page,
								pages_step: pages_step
							});
						}
					});

				}
			});
		}
	});

	$("#join_tuan_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/tuans/join_tuan",
			data: {tuan_id: $("#tuanid").val(),
			type: "get"},
			success: function(html)
			{
				$("#join_tuan_div").html(html);
			}
		});
	});

	$("#modify_url_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/tuans/modify_pay_url",
			data: {tuan_id: $("#tuanid").val(),
			type: "get"},
			success: function(html)
			{
				$("#modify_pay_url_div").html(html);
			}
		});
	});

	$("#notify_deliver_button").click(function(){
		$.ajax({
			type: "POST",
			url: "/tuans/notify_deliver",
			data: {tuan_id: $("#tuanid").val(),
			type: "get"},
			success: function(html)
			{
				$("#notify_deliver_div").html(html);
			}
		});
	});
});
